package com.hqyj.javaSpringBoot.modules.account.dao;

import com.hqyj.javaSpringBoot.modules.account.entity.User;
import com.hqyj.javaSpringBoot.modules.account.entity.UserSearchBean;
import com.hqyj.javaSpringBoot.modules.common.entiy.SearchBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserDao {

    @Insert("insert into account_user (create_date,email,password,user_image,user_name) " +
            "values (#{createDate},#{email},#{password},#{userImage},#{userName})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insertUser(User user);

    @Delete("delete from account_user where id=#{id}")
    void deleteUser(int id);

    @Update("update account_user set email=#{email}," +
            "user_image=#{userImage},user_name=#{userName} where id=#{id}")
    void updateUser(User user);

    @Update("update account_user set password=#{password} where id=#{id}")
    void updateUserPassword(User user);

    /**注册验证*/
    @Select("select * from account_user where user_name = #{userName} or email = #{email}")
    List<User> getUserByUserNameorEmail(@Param("userName")String userName,
                                        @Param("email")String email);
    /**登录验证*/
    @Select("select * from account_user where user_name = #{userName} and password = #{password}")
    List<User> getUserByUserNameAndPassword(@Param("userName")String userName,
                                      @Param("password")String password);

    @Select("select * from account_user where id=#{id}")
    @Results(id="userResult",value = {
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "roles",javaType = List.class,
            many=@Many(select = "com.hqyj.javaSpringBoot.modules.account.dao.RoleDao.getRolesByUserId"))
    })
    User getUserByUserId(int id);

    @Select("select * from account_user where user_name=#{userName}")
    User getUserByUserName(String userName);

    @Select("<script>" +
            "select * from account_user" +
            " <where> " +
                "<if test ='keyWord != \"\" and keyWord !=null'>" +
                    " email like '%${keyWord}%' or user_name like '%${keyWord}%'" +
                "</if>" +
            "</where>" +
            "<choose>" +
                "<when test='orderBy !=\"\" and orderBy !=null'>" +
                    " order by ${orderBy} ${direction}" +
                "</when>" +
                "<otherwise>" +
                    " order by id" +
                "</otherwise>" +
            "</choose>" +
            "</script>")
    List<User> getUserBySearch(UserSearchBean userSearchBean);

    /**
     * 查询所有的用户信息
     * @return
     */
    @Select("select * from account_user")
    List<User> getUsers();
}
